from rest_framework.viewsets import GenericViewSet

from apps.post.filters import PostsFilter, TopicsFilter, UserPostsFilter, UserTopicsFilter
from apps.post.models import Post, Type, Topic
from apps.post.serializers import PostsSerializer, TypesSerializer, TopicsSerializer, FileSerializer
from utils.Pagination import CustomPagination
from utils.permission import DeletePermission
from utils.authentications import ExpiringTokenAuthentication
from utils.viewset import CustomList, CustomCreate, CustomRetrieve, CustomDestroy, UploadAPIView

""" 在as_view映射请求
ModelViewSet 数据返回
重写返回数据格式
"""


class PostsView(CustomList, CustomRetrieve, GenericViewSet):
    """
    查看posts:
        list retrieve
    """
    queryset = Post.objects.order_by('-pub_time')
    serializer_class = PostsSerializer
    pagination_class = CustomPagination
    filter_backends = [PostsFilter]


class UserPostsView(CustomList, CustomCreate, CustomRetrieve, CustomDestroy, GenericViewSet):
    """
    操作posts:
        list create retrieve destroy
    """
    queryset = Post.objects.order_by('-pub_time')
    serializer_class = PostsSerializer
    pagination_class = CustomPagination
    filter_backends = [UserPostsFilter]
    permission_classes = [DeletePermission]
    authentication_classes = [ExpiringTokenAuthentication]


class UserTypesView(CustomList, CustomCreate, CustomRetrieve):
    """
    types：
        list create retrieve
    """
    queryset = Type.objects.all()
    serializer_class = TypesSerializer
    authentication_classes = [ExpiringTokenAuthentication]


class TopicsView(CustomList, CustomRetrieve, GenericViewSet):
    """
    topics：
        list create retrieve
    """
    queryset = Topic.objects.order_by('-pub_time')
    serializer_class = TopicsSerializer
    pagination_class = CustomPagination
    filter_backends = [TopicsFilter]


class UserTopicsView(CustomList, CustomCreate, CustomRetrieve, CustomDestroy, GenericViewSet):
    """
    topics：
        list create retrieve
    """
    queryset = Topic.objects.order_by('-pub_time')
    serializer_class = TopicsSerializer
    pagination_class = CustomPagination
    filter_backends = [UserTopicsFilter]
    authentication_classes = [ExpiringTokenAuthentication]
    permission_classes = [DeletePermission]


class Upload(UploadAPIView):
    """
    upload
    """
    serializer_class = FileSerializer
    authentication_classes = [ExpiringTokenAuthentication]
